package com.woniuxy.kaer.shop.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.kaer.commons.jwtUtil.JwtUtil;
import com.woniuxy.kaer.commons.util.PageInfo;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.form.AddOrderForm;
import com.woniuxy.kaer.form.PageForm;
import com.woniuxy.kaer.form.SearchOrderForm;
import com.woniuxy.kaer.param.AddOrderParam;
import com.woniuxy.kaer.param.SearchOrderParam;
import com.woniuxy.kaer.shop.model.ProductOrder;
import com.woniuxy.kaer.shop.service.ProductOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>
 *  商品订单
 * </p>
 *
 * @author 郭建东
 * @since 2025年02月17日
 */
@RestController
@RequestMapping("/productOrder")
@Api(tags = "商品订单管理")
public class ProductOrderController {

    @Resource
    private ProductOrderService productOrderService;

    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/addOrder")
    @ApiOperation("下单")
    public Result addOrder(@RequestBody AddOrderForm form, @RequestHeader("token") String token) throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        return Result.succeed("下单成功",productOrderService.addOrder(BeanUtil.toBean(form, AddOrderParam.class),userId));
    }

    //支付
    @PostMapping("/pay")
    @ApiOperation("支付")
    public Result pay(String productOrderId,@RequestHeader("token") String token)throws Exception{

        Integer userId = jwtUtil.getUserId(token);
        return Result.succeed("支付成功", productOrderService.pay(productOrderId,userId));
    }

    //支付
    @PostMapping("/notify")
    public String payNotify(HttpServletRequest request)throws Exception{
        System.out.println(111);
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();){
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        productOrderService.handleOrder(params);
        return "SUCCESS";
    }

    @PostMapping("/confirmOrder")
    @ApiOperation("确认收货")
    public Result confirmOrder(String productOrderId,@RequestHeader("token") String token)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        productOrderService.confirmOrder(productOrderId,userId);
        return Result.succeed("确认收货成功",null);
    }

    //管理员发货，修改订单状态
    @PostMapping("/shipments")
    @ApiOperation("发货")
    public Result shipments(String productOrderId)throws Exception{
        productOrderService.shipments(productOrderId);
        return Result.succeed("发货成功",null);
    }

    @PostMapping("/searchOrder")
    @ApiOperation("管理员查询订单")
    public Result searchOrder(@RequestBody SearchOrderForm form)throws Exception{
        PageInfo productOrderPageInfo = productOrderService.searchOrders(BeanUtil.toBean(form, SearchOrderParam.class),null);
        return Result.succeed("查询成功",productOrderPageInfo);
    }

    @PostMapping("/searchMyOrder")
    @ApiOperation("用户查询订单")
    public Result searchMyOrder(@RequestBody SearchOrderForm form, @RequestHeader("token") String token)throws Exception{
        Integer userId = jwtUtil.getUserId(token);
        PageInfo productOrderPageInfo = productOrderService.searchOrders(BeanUtil.toBean(form, SearchOrderParam.class), userId);
        return Result.succeed("查询成功",productOrderPageInfo);
    }




}

